package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.DataManager;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.CertInfo;
import com.epfresh.supplier.entity.Store;
import com.epfresh.supplier.entity.StoreBaseInfo;
import com.google.gson.Gson;

public class SelectNameEnsureActivity extends BaseActivity {
    private TextView personTv;
    private TextView companyTv;
    private TextView tvTitle;
    Context mContext;
    //type == 1,实名认证,type==2,银行卡认证
    int type = 1;
    String certType;
    //0,个人;1,企业
    String certPersonCompanyType;
    String merchantId;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_name_ensure);
        if (savedInstanceState != null) {
            getIntent().putExtra("type", savedInstanceState.getInt("type"));
            getIntent().putExtra("isFirst", savedInstanceState.getBoolean("isFirst"));
            getIntent().putExtra("certInfo", savedInstanceState.getString("certInfo"));
            getIntent().putExtra("certType", savedInstanceState.getString("certType"));
            getIntent().putExtra("merchantId", savedInstanceState.getString("merchantId"));
            getIntent().putExtra("certPersonCompanyType", savedInstanceState.getString("certPersonCompanyType"));
            getIntent().putExtra("card_count", savedInstanceState.getInt("card_count"));
            getIntent().putExtra("isMineIn", savedInstanceState.getBoolean("isMineIn"));
        }
        certType = getIntent().getStringExtra("certType");
        certPersonCompanyType = getIntent().getStringExtra("certPersonCompanyType");
        type = getIntent().getIntExtra("type", 1);
        mContext = this;
        ivBack.setVisibility(View.VISIBLE);
        personTv = (TextView) findViewById(R.id.person);
        companyTv = (TextView) findViewById(R.id.company);
        tvTitle = (TextView) findViewById(R.id.tv_title_);

        merchantId = getIntent().getStringExtra("merchantId");
        String html = "<font>实名信息后会显示 </font><img src=‘img1’><font> / </font><img src=‘img2’><font> ，会增加店铺可信度，增加销量</font>";
        CharSequence text = Html.fromHtml(html, new Html.ImageGetter() {

            public Drawable getDrawable(String source) {
                //根据图片资源ID获取图片

                if (source.equals("‘img1’")) {
                    Drawable draw = getResources().getDrawable(R.mipmap.pr_logo);
                    int h = LocalDisplay.dp2px(13);
                    int w = draw.getIntrinsicWidth() * h / draw.getIntrinsicHeight();
                    draw.setBounds(0, 0, w, h);
                    return draw;
                } else if (source.equals("‘img2’")) {
                    Drawable draw = getResources().getDrawable(R.mipmap.co_logo);
                    int h = LocalDisplay.dp2px(13);
                    int w = draw.getIntrinsicWidth() * h / draw.getIntrinsicHeight();
                    draw.setBounds(0, 0, w, h);
                    return draw;
                }
                return null;
            }
        }, null);
        tvTitle.setText(text);


        personTv.setOnClickListener(this);
        companyTv.setOnClickListener(this);
        if (type == 1) {
            String s = "实名信息 <font color=\"#ff0000\">*</font>";
            toolbarTitle.setText(Html.fromHtml(s));
        } else {
            toolbarTitle.setText("银行信息");
            personTv.setText("个人账户");
            companyTv.setText("企业账户");
        }
        String certInfo = getIntent().getStringExtra("certInfo");
        if (certInfo != null && !"".equals(certInfo)) {
            CertInfo ci = new Gson().fromJson(certInfo, CertInfo.class);
            DataManager.getInstance().setCertInfo(ci);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putInt(Constant.TYPE, getIntent().getIntExtra(Constant.TYPE, 1));
        outState.putInt("card_count", getIntent().getIntExtra("card_count", 1));
        outState.putString("certInfo", getIntent().getStringExtra("certInfo"));
        outState.putString("certType", getIntent().getStringExtra("certType"));
        outState.putString("certPersonCompanyType", getIntent().getStringExtra("certPersonCompanyType"));
        outState.putString("merchantId", getIntent().getStringExtra("merchantId"));
        outState.putBoolean("isFirst", getIntent().getBooleanExtra("isFirst", false));
        outState.putBoolean("isMineIn", getIntent().getBooleanExtra("isMineIn", false));

        super.onSaveInstanceState(outState);
    }


    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.person:
//                reqCertInfo();
                reqCertInfo("1");
//                intoPersonal();
                break;
            case R.id.company:
                reqCertInfo("2");
//                intoCompany();
//                reqCertInfo();
                break;
        }
    }

    private void intoCompany() {


        if (type == 1) {
            //审核被拒，且填写顿的是个人认证
            if ("3".equals(certType) && "0".equals(certPersonCompanyType)) {
//                        toast("当前认证是企业认证如需修改为个人认证请联系客服");
                toast("如需修改，请联系客服：400-0588-198");
                return;
            }
            Intent company = new Intent(this,
                    EnsureCompanyActivity.class);
            company.putExtra("certType", getIntent().getStringExtra("certType"));
            company.putExtra("certInfo", getIntent().getStringExtra("certInfo"));
            company.putExtra("merchantId", getIntent().getStringExtra("merchantId"));
            company.putExtra("isFirst", getIntent().getBooleanExtra("isFirst", false));
            startActivity(company);
        } else {
            intoBank(1, 2);
        }
    }

    private void intoPersonal() {
        if (type == 1) {
            //审核被拒，且填写顿的是企业认证
            if ("3".equals(certType) && "1".equals(certPersonCompanyType)) {
//                        toast("当前认证是个人认证如需修改为企业认证请联系客服");
                toast("如需修改，请联系客服：400-0588-198");
                return;
            }
            Intent intent = new Intent(this,
                    EnsurePersonActivity.class);
            intent.putExtra("certType", getIntent().getStringExtra("certType"));
            intent.putExtra("certInfo", getIntent().getStringExtra("certInfo"));
            intent.putExtra("merchantId", getIntent().getStringExtra("merchantId"));
            intent.putExtra("isFirst", getIntent().getBooleanExtra("isFirst", false));

            Log.e("ci", "" + getIntent().getBooleanExtra("isFirst", false));
            Log.e("ci", "" + getIntent().getStringExtra("certInfo"));
            startActivity(intent);
        } else {
            intoBank(-1, 1);
        }
    }

    private void intoBank(int type, int userType) {
        Intent bank = new Intent(this, BankActivity.class);
        bank.putExtra("card_count", getIntent().getIntExtra("card_count", 1));
        bank.putExtra(Constant.TYPE, type);
        bank.putExtra("userType", userType);
        bank.putExtra("isFirst", getIntent().getBooleanExtra("isFirst", false));
        startActivity(bank);
    }


    private void reqCertInfo(String type) {
        RequestEntityMapSupply RequestEntitySupply = new RequestEntityMapSupply();
        RequestEntitySupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_merchant_cert);
        if (merchantId == null || "".equals(merchantId)){
            merchantId = BaseApplication.getInstance().getMerchantId();
        }
        RequestEntitySupply.putParameter("merchantId", merchantId);
        RequestTag tag = new RequestTag();
        tag.type = RequestTag.TYPE_CURRENT;
        tag.arg1 = type;
        request(RequestEntitySupply, tag, onCertInfoRequestListener);
    }

    private OnRequestListener<CertInfo> onCertInfoRequestListener = new OnRequestListener<CertInfo>() {
        @Override
        public CertInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, CertInfo.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            if (failCode == HttpRequest.FAIL_JSON_PARSER) {
                if (tag != null && tag instanceof RequestTag) {
                    RequestTag rt = (RequestTag) tag;
                    DataManager.getInstance().setCertInfo(null);

                    if ("1".equals(rt.arg1)) {
                        intoPersonal();
                    } else if ("2".equals(rt.arg1)) {
                        intoCompany();
                    }

                }
            } else {
                toast(arg + "");
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            toast(arg + "");
        }

        @Override
        public void onStart(Object tag) {

            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<CertInfo> response, Object tag) {

            hideProgressDialog();
            if (tag != null && tag instanceof RequestTag) {
                RequestTag rt = (RequestTag) tag;
                CertInfo ci = response.getResponseElement();
                DataManager.getInstance().setCertInfo(ci);

                if ("1".equals(rt.arg1)) {
                    intoPersonal();
                } else if ("2".equals(rt.arg1)) {
                    intoCompany();
                }

            }
        }



    };
}
